   <template>
  <el-card>
    <el-row type="flex" justify="space-between">
      <el-col>
        <el-form :inline="true">
          <el-form-item>
            <el-input
              placeholder="标签名称"
              v-model="queryData.tag_name"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-select
              placeholder="归属学科"
              v-model="queryData.subject_id"
              @change="changeFn"
            >
              <el-option
                v-for="item in subjectList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select placeholder="归属目录" v-model="queryData.directory_id">
              <el-option
                v-for="item in directoryList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select v-model="queryData.is_show" placeholder="显示状态">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="initData" plain>搜索</el-button>
            <el-button @click="resetSearch">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col style="width: 100px">
        <el-button type="primary" @click="showDialog = true" plain
          >创建标签</el-button
        >
      </el-col>
    </el-row>
    <!-- 表格 -->
    <el-table
      :data="list"
      border
      :header-cell-style="{ color: '#64646d', backgroundColor: '#fafafa' }"
    >
      <el-table-column label="序号" width="50" type="index"> </el-table-column>
      <el-table-column prop="tag_name" label="标签名称"> </el-table-column>
      <el-table-column prop="directory_name" label="归属目录">
      </el-table-column>
      <el-table-column prop="subject_name" label="归属学科"> </el-table-column>
      <el-table-column prop="username" label="创建人"> </el-table-column>
      <el-table-column prop="" label="是否显示">
        <template slot-scope="{ row }">
          <el-switch
            v-model="row.is_show == 1 ? true : false"
            active-text="是"
            inactive-text="否"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button
            @click="editHandler(row)"
            type="primary"
            circle
            icon="el-icon-edit"
          ></el-button>
          <el-button
            @click="delHandler(row.id)"
            type="danger"
            circle
            icon="el-icon-delete"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryData.page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="queryData.limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 新增弹框区域 -->
    <el-dialog
      v-if="showDialog"
      :title="this.formData.id ? '修改标签' : '添加标签'"
      :visible.sync="showDialog"
      @close="closeHandler"
    >
      <el-form :model="formData" :rules="formRules" ref="form" :inline="true">
        <el-row>
          <el-form-item prop="subject_id" label="归属学科">
            <el-select
              placeholder="归属学科"
              v-model="formData.subject_id"
              @change="changeFn"
            >
              <el-option
                v-for="item in subjectList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="directory_id" label="归属目录">
            <el-select placeholder="归属目录" v-model="formData.directory_id">
              <el-option
                v-for="item in directoryList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item prop="tag_name" label="标签名称">
            <el-input
              v-model="formData.tag_name"
              placeholder="请输入标签名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input-number
              v-model="formData.sort"
              label="描述文字"
            ></el-input-number>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item prop="is_show" label="是否显示">
            <el-switch
              v-model="formData.is_show"
              active-text="是"
              inactive-text="否"
              :active-value="1"
              :inactive-value="2"
            >
            </el-switch>
          </el-form-item>
        </el-row>
      </el-form>

      <el-row type="flex" justify="end">
        <el-button @click="showDialog = false" size="small">取 消</el-button>
        <el-button type="primary" size="small" @click="btnOk">提交</el-button>
      </el-row>
    </el-dialog>
  </el-card>
</template>
<script>
import {
  getTagInfoApi,
  getTagListApi,
  delTagApi,
  addTagApi,
  editTagApi,
  getSubjectTreeApi,
} from "@/api/subject/tag";

export default {
  name: "tag",
  data() {
    return {
      // 表格数据
      list: [],
      subjectList: [],
      directoryList: [],

      // 请求列表数据
      queryData: {
        tag_name: "",
        subject_id: "",
        directory_id: "",
        is_show: "",
        page: 1,
        limit: 10,
      },
      total: 0,
      // 新增
      formData: {
        tag_name: "",
        subject_id: "",
        directory_id: "",
        is_show: "",
        sort: "",
      },
      formRules: {
        subject_id: [
          {
            required: true,
            message: "请选择学科",
            trigger: "change",
          },
        ],
        directory_id: [
          {
            required: true,
            message: "请选择目录",
            trigger: "change",
          },
        ],
        tag_name: [
          {
            required: true,
            message: "请输入标签名称",
            trigger: "blur",
          },
        ],
        sort: [
          {
            required: true,
            message: "请选择",
            trigger: "change",
          },
        ],
        is_show: [
          {
            required: true,
            message: "请输入",
            trigger: "change",
          },
        ],
      },
      showDialog: false,
      options: [
        {
          value: "1",
          label: "显示",
        },
        {
          value: "2",
          label: "隐藏",
        },
      ],
    };
  },
  created() {
    this.initData();
    // 如果是跳转来的  则接受初始化参数
    if (this.$route.query) {
      this.queryData.subject_id = this.$route.query.subject_id;
      this.queryData.directory_id = this.$route.query.directory_id;
      // this.changeFn(this.queryData.subject_id);
    }
  },
  // watch: {
  //   "queryData.subject_id": {
  //     handler(newVal, oldVal) {
  //       console.log(newVal); // 打印name变化的值
  //       this.queryData.directory_id = "";
  //     },
  //     immediate: true,
  //     deep: true,
  //   },
  // },
  methods: {
    async initData() {
      let res = await getSubjectTreeApi();
      // console.log(res);
      this.subjectList = res;
      let { list, total } = await getTagListApi(this.queryData);
      // console.log(list);
      this.list = list;
      this.total = total;
      // 如果是跳转来的  则接受初始化参数
      // if (this.$route.query) {
      //   this.queryData.subject_id = this.$route.query.subject_id;
      //   this.queryData.directory_id = this.$route.query.directory_id;
      this.changeFn(this.queryData.subject_id, true);
      // }
    },
    handleSizeChange(val) {
      // console.log(val);
      this.queryData.limit = val;
      this.initData;
    },
    handleCurrentChange(val) {
      // console.log(val);
      this.queryData.page = val;
      this.initData();
    },
    // 搜索重置
    resetSearch() {
      this.queryData.tag_name = "";
      this.queryData.subject_id = "";
      this.queryData.directory_id = "";
      this.queryData.is_show = "";
      this.initData();
    },
    // 新增
    async btnOk() {
      try {
        await this.$refs.form.validate();
      } catch (error) {
        return console.log(error);
      }

      if (this.formData.id) {
        await editTagApi(this.formData);
      } else {
        try {
          await addTagApi(this.formData);
        } catch (error) {
          console.log(error);
        }
      }
      this.$message.success("操作成功");
      this.showDialog = false;
      this.initData();
    },
    closeHandler() {
      this.formData.tag_name = "";
      this.formData.subject_id = "";
      this.formData.directory_id = "";
      this.formData.is_show = "";
      this.formData.sort = "";
    },
    // 修改
    async editHandler(val) {
      console.log(val);
      let res = await getTagInfoApi({ id: val.id });
      this.formData = res;
      console.log(res);
      this.changeFn(res.subject_id, false);

      this.showDialog = true;
    },

    // 删除
    async delHandler(id) {
      await this.$confirm("确认删除吗,是否继续");
      await delTagApi(id);
      this.$message.success("删除成功");
      this.initData();
    },
    // 下拉框二级联动
    changeFn(value, flag) {
      // if (this.subject_id !== value) {
      //   this.queryData.directory_id = "";
      // }

      // console.log(value);
      console.log(this.subjectList.length);
      for (let i = 0; i < this.subjectList.length; i++) {
        let oneS = this.subjectList[i];
        if (value === oneS.id) {
          // console.log(oneS);
          this.directoryList = oneS.children;
          if (!flag) {
            this.queryData.directory_id = "";
          }
        }
        // this.queryData.directory_id = "";
      }
    },
  },
};
</script>

<style>
</style>
